#include "mainwindow.h"
#include "./ui_mainwindow.h"
#include<QMenu>
#include<QEvent>
#include<QMessageBox>
#include<QDebug>
#include<QDateTime>
#include<QString>
#include<QLineEdit>
#include<QKeyEvent>
// #include<QNetworkAccessManager>
MainWindow* MainWindow::mywidget = nullptr;
MainWindow::MainWindow(QWidget* parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    mywidget = this;
    //*初始化各个类
    mFetcher = new WeatherFetcher(this);
    mParseJsonInfo = new ParseJsonInfo(this);
    mUpdateUI = new UpdateUI(this);
    mDrawCurve = new DrawCurve(this);

    //*设置信号连接，实现类的对接
    connect(mFetcher, &WeatherFetcher::weatherDataReceived, mParseJsonInfo, &ParseJsonInfo::parseJson);
    connect(mParseJsonInfo, &ParseJsonInfo::parseCompleted, mUpdateUI, &UpdateUI::updateUI);
    connect(mParseJsonInfo, &ParseJsonInfo::parseCompleted, mDrawCurve, &DrawCurve::updateData);
    //*处理天气错误
    connect(mFetcher, &WeatherFetcher::weatherError, this, [=](QString error) {
        QMessageBox::warning(this, "错误", error, QMessageBox::Ok);
        });

    //*设置窗口属性
    setWindowFlags(Qt::FramelessWindowHint);//无边框
    setFixedSize(width(), height());
    //*设置退出界面
    mExitMenu = new QMenu(this);
    mExitAction = new QAction();
    mExitAction->setText("退出");
    mExitAction->setIcon(QIcon("://res/close.png"));
    mExitMenu->addAction(mExitAction);//主要退出行为

    mRefreshAct = new QAction();
    mRefreshAct->setText("刷新");
    mRefreshAct->setIcon(QIcon("://res/refresh.png"));
    mExitMenu->addAction(mRefreshAct);

    //优化退出体验
    connect(mExitAction, &QAction::triggered, this, [=]
        {
            QMessageBox msgBox(this);
            msgBox.setWindowTitle("退出");
            msgBox.setText("确认要退出吗？");
            msgBox.setIcon(QMessageBox::Question); // 保留问号图标
            msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
            msgBox.setButtonText(QMessageBox::Yes, "我要退出");
            msgBox.setButtonText(QMessageBox::No, "手滑了");

            int ret = msgBox.exec();
            if (ret == QMessageBox::Yes) {
                qApp->exit(0);
            }
        });

    // 新功能：回车查询天气
    // connect(ui->leCity, &QLineEdit::returnPressed, this, &MainWindow::on_btnSearch_clicked);

    connect(mRefreshAct, &QAction::triggered, this, [=]
        {
            if (ui->leCity->text().isEmpty())
            {
                mFetcher->getWeatherInfo("北京");
                QMessageBox::warning(this, "刷新", "刷新成功", QMessageBox::Ok);
            }
            else
            {
                mFetcher->getWeatherInfo(ui->leCity->text());
                ui->leCity->clear();
            }
        });
    //*默认传入武汉城市天气信息

    mFetcher->getWeatherInfo("武汉");
}

void MainWindow::keyPressEvent(QKeyEvent* event) {
    switch (event->key()) {
    case Qt::Key_Enter:
    case Qt::Key_Return: {
        QString city = ui->leCity->text();
        mFetcher->getWeatherInfo(city);
        ui->leCity->clear(); 

        break;
    }
    default:
        break;
    }
}


MainWindow::~MainWindow()
{
    delete ui;
}



void MainWindow::contextMenuEvent(QContextMenuEvent* event)
{
    if (!event)
        return; // 确保 event 不为空

    mRefreshAct->setText(ui->leCity->text().isEmpty() ? "刷新" : "查询");
    

    // 弹出右键菜单
    mExitMenu->exec(QCursor::pos());

    // 事件已经处理，不需要向上报告
    event->accept();
}

void MainWindow::mouseMoveEvent(QMouseEvent* event)
{
    this->move(event->globalPos() - moffset);
}

void MainWindow::mousePressEvent(QMouseEvent* event)
{
    moffset = event->globalPos() - this->pos();

}

void MainWindow::on_btnSearch_clicked()
{
    QString city = ui->leCity->text();
    mFetcher->getWeatherInfo(city);
    ui->leCity->clear(); // 回车后清空输入框
}

